<template>
  <div class="xtx-goods-page" v-if="goods.id">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[0].parent.id}`">
          {{ goods.categories[0].parent.name }}
        </XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
          <goods-image :pictures="goods.mainPictures"></goods-image>
          <goods-sales></goods-sales>
        </div>
        <!-- 商品信息右边 -->
        <div class="goods-info-right">
          <goods-name :goods="goods"></goods-name>
          <goods-sku
            :specs="goods.specs"
            :skus="goods.skus"
            skuId="1369155862131642369"
          ></goods-sku>
          <!--  :max="goods.inventory" -->
          <XtxNumbox label="数量" v-model="num" />
          <XtxButton type="primary" style="margin-top:20px;"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <goods-tabs></goods-tabs>
          <!-- 注意事项 -->
          <goods-warn></goods-warn>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goods-hot :goodsId="goods.id" :type="1"></goods-hot>
          <goods-hot :goodsId="goods.id" :type="2"></goods-hot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { computed, onBeforeMount, provide, ref } from 'vue'
import GoodsRelevant from './components/goods-relevant'
import { findGoodsAPI } from '@/api/goods'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn
  },
  setup () {
    const goods = ref([])
    const num = ref(1)
    const route = useRoute()
    const id = computed(() => route.params.id)

    provide('goods', goods)

    onBeforeMount(async () => {
      goods.value = await findGoodsAPI(id.value)
      const arr = goods.value.specs.map(item =>
        item.values.map(item2 => ({ ...item2, select: false, disabled: false }))
      )
      arr.map((item, index) => (goods.value.specs[index].values = item))
    })

    return { goods, num }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
